<?php
/**
 * QueryCache ver 1.0
 * Created by Data-Diggers.com
 * Website:             http://www.data-diggers.com/
 * Copyright notice: This is free software. GNU license.
 * This code is provided as is. We don't take any responsibility for damage
 * done by use of this software.
 * Feel free to change and redistribute this code, but leave reference to our
 * website.
 */
 
if (!defined('IS_ADMIN_FLAG')) {
 die('Illegal Access');
}

class QueryCache {
	function QueryCache() {
		$this->queries = array();
	}
	
	// cache queries if and only if query is 'SELECT' statement
	// returns:
	//	TRUE - if and only if query has been stored in cache
	//	FALSE - otherwise
	function cache($query, $result) {
		if( $this->isSelectStatement($query) === TRUE) {
			$this->queries[$query]['result'] = $result;
			$this->queries[$query]['performed'] = TRUE;
		} else {
			return(FALSE);
		}
		return(TRUE);
	}
	
	function getFromCache($query) {
		$ret = $this->queries[$query]['result'];
		mysql_data_seek($ret, 0);
		return($ret);
	}

	function inCache($query) {
		return(isset($this->queries[$query]['performed']));
	}
	
	function isSelectStatement($query) {
        $pos = strpos($query,"select");
		if( $pos !== false && $pos == 0 ) {
			return(TRUE);
		}
        return(false);
	}
}
?>